﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace WebAPITest2.Controllers
{
    public class TestController : ApiController
    {
        [HttpPost]
        [Route("api/process")]
        public async Task<HttpResponseMessage> ProcessRequest() // 修改返回类型为 HttpResponseMessage
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "Unsupported media type");
            }

            var provider = new MultipartFormDataMemoryProvider();

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                string senderCode = provider.FormData["senderCode"];
                string actionType = provider.FormData["actionType"];
                string dataJson = provider.FormData["data"];

                if (string.IsNullOrEmpty(senderCode) ||
                    string.IsNullOrEmpty(actionType) ||
                    string.IsNullOrEmpty(dataJson))
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Missing required parameters");
                }

                dynamic data = JsonConvert.DeserializeObject(dataJson);

                // 处理业务逻辑（略）

                // 直接返回对象，Web API 会自动序列化为 JSON
                ReturnDataModel2 model = new ReturnDataModel2
                {
                    success = true,
                    code = 0,                   // 注意添加缺失的 code 字段
                    message = "Processed",
                    tipType = 1,
                    data = new Data
                    {
                        recID = 19901,
                        taskNum = "202505080909"
                    }
                };

                // 返回 JSON 对象，并设置状态码
                return Request.CreateResponse(HttpStatusCode.OK, model);
            }
            catch (JsonException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid JSON data: " + ex.Message);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        public class ReturnDataModel2
        {
            public bool success { get; set; }
            public int code { get; set; }
            public string message { get; set; }
            public Data data { get; set; }
            public int tipType { get; set; }
            
        }
        public class Data
        {
            public int recID { get; set; }
            public string taskNum { get; set; }
        }

        // 辅助类：内存存储 form-data
        public class MultipartFormDataMemoryProvider : MultipartFormDataStreamProvider
        {
            public MultipartFormDataMemoryProvider() : base("~/") { }

            public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
            {
                // 防止写入文件到磁盘（纯内存处理）
                return string.Empty;
            }
        }
    }
}

